package com.edu.imau;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

//连接池
public class ConnectionManager {
    private static Connection connection = null;

    public static Connection getConnection() {
        Connection connection = null;
        try {
            //1:
            InputStream inputStream = ConnectionManager.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            String driver = properties.getProperty("driver");
            Class.forName(driver);
            //2
            if (connection != null)
                return connection;
            String url = properties.getProperty("url");//localhost->39.104.51.96
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            connection = DriverManager.getConnection(url, user, password);

        } catch (ClassNotFoundException e) {
            System.out.println("数据驱动不存在");
            e.printStackTrace();
        } catch (SQLException throwables) {
            System.out.println("数据库连接失败");
            throwables.printStackTrace();
        } catch (IOException e) {
            System.out.println("读取数据库配置出现异常！");
            e.printStackTrace();
        }
        return connection;
    }


}

